﻿using Aliphant.Mvvm.XEvent.Events;
using Aliphant.Mvvm.XEvent.UI;

using CuteWallpaperDemo.Models;

using DataModel.Contants;
using DataModel.Models;

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;

namespace CuteWallpaperDemo.ViewModels
{
    public class SelectWindowViewModel : BindableBase
    {
        private ProjectAgent _projectAgent;
        private WindowEvent _windowEvent;

        public SelectWindowViewModel()
        {
            _projectAgent = ProjectContainer.ProjectAgent;
            _windowEvent = _projectAgent.EventManager.GetEvent<WindowEvent>();
        }

        private string _pictureUrl = string.Empty;
        public string PictureUrl { get => _pictureUrl; set { SetProperty(ref _pictureUrl, value); } }

        /// <summary>
        /// 选择图片
        /// </summary>
        public ICommand SelectedPictureCommand
        {
            get => new RelayCommand<object>(obj =>
            {
                Action<string> callback = r =>
                {
                    PictureUrl = r;
                    //Name = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
                    //Name = Path.GetFileNameWithoutExtension(r);
                };

                _windowEvent.Publish(new WindowEvent
                {
                    Data = FileFilter.ALL,
                    CallBack = callback,
                    WindowActionType = WindowActionType.Edit,
                    EventId = EventIds.SelectFileDialog,
                });
            });
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        public ICommand ConfirmCommand
        {
            get => new RelayCommand<object>(obj =>
            {
                if (string.IsNullOrWhiteSpace(PictureUrl))
                {
                    MessageBox.Show("未选择图片", "提示");
                    return;
                }

                var files = PictureUrl.Split(';');

                _windowEvent.Publish(new WindowEvent
                {
                    Data = files,
                    WindowActionType = WindowActionType.OK,
                    EventId = EventIds.OpenAddWindow,
                });

            });
        }

        /// <summary>
        /// 取消
        /// </summary>
        public ICommand CancelCommand
        {
            get => new RelayCommand<object>(obj =>
            {
                _windowEvent.Publish(new WindowEvent
                {
                    WindowActionType = WindowActionType.Close,
                    EventId = EventIds.OpenAddWindow,
                });
            });
        }


        private void SetMessageBox(string message, string? title = null)
        {
            message = message ?? string.Empty;
            title = title ?? string.Empty;

            Application.Current.Dispatcher.Invoke(() =>
            {
                MessageBox.Show(message, title);
            });
        }

        private Project CreateProject(string pictureUrl, int dir = 0)
        {
            string title = Path.GetFileNameWithoutExtension(pictureUrl);
            return new Project
            {
                Dir = DateTimeOffset.Now.ToUnixTimeSeconds().ToString() + dir,
                Origin = pictureUrl,
                Title = title,
                Path = "high" + FileExtension.BMP,
                Middle = "middle" + FileExtension.BMP,
                Preview = "preview" + FileExtension.BMP
            };
        }
    }
}
